function genStr(num) {
    let list = new Array(num);
    let start = ('A').charCodeAt();
    let limit = 57;
    while (num-- > 0) {
        list.push(String.fromCharCode(start + parseInt(limit * Math.random())));
    }
    return list.join('');
}

function strEqual(str1, str2) {
    return str1 == str2;
}

function genNextArr(str) {
    let next = new Array(str.length);
    let k = -1,
        j = 0;
    next[0] = -1;

    while (j < next.length - 1) {
        if (k == -1 || str[k] == str[j]) {
            k++;
            j++;
            next[j] = k;
        } else {
            k = next[k];
        }
    }

    next[0] = 0;
    return next;
}

function kmp(str1, str2) {
    let res = -1;
    let j = 0;
    let next = genNextArr(str2);
    let slen = str1.length;
    let tlen = str2.length;

    for (let i = 0; i < slen; i++) {
        if (str1[i] == str2[j]) {
            if (j == tlen - 1) {
                res = i - j;
                break;
            }
            j++;
        } else {
            j = next[j];
        }
    }
    return res;
}

// let str1 = genStr(100);
// let str2 = genStr(20);
// let str1 = 'a';
// let str2 = 'a';
let str1 = '1112111ababaaaba123';
let str2 = 'ababaaaaba';
[0, 0, 0, 1, 2, 3, ]
console.log(str1)
console.log(str2)
console.log(genNextArr(str2));
console.log(kmp(str1, str2))